package com.jmz.apt.api.service.user;

import com.jmz.apt.api.dao.AdminRoleDao;
import com.jmz.apt.api.dao.AdminUserRoleDao;
import com.jmz.apt.api.entity.dbEntity.JmzAdminRole;
import com.jmz.apt.api.entity.dbEntity.JmzAdminUser;
import com.jmz.apt.api.entity.dbEntity.JmzAdminUserRoles;
import com.jmz.apt.api.enums.ResponseCode;
import com.jmz.apt.api.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

/**
 * Created by fanglexue on 2019/11/6.
 */

@Component
public class AdminUserRole {

    @Autowired
    AdminRoleDao adminRoleDao;

    @Autowired
    AdminUserRoleDao adminUserRoleDao;


    public boolean saveAdminUserRoles(JmzAdminUserRoles jmzAdminUserRoles)throws DataAccessException{
        JmzAdminRole jmzAdminRole = adminRoleDao.findRoleOne(jmzAdminUserRoles.getRoleId());
        if(jmzAdminRole == null){
            return false;
        }
        JmzAdminUserRoles jmzAdminUserRoles1 = adminUserRoleDao.findOne(jmzAdminUserRoles);
        Integer roleId = 0;
        if(jmzAdminUserRoles1 != null){
             roleId = jmzAdminUserRoles1.getRoleId();
         }else{
            roleId = adminUserRoleDao.saveAdminUserRole(jmzAdminUserRoles);
        }
         return   roleId != null? true : false;
    }


    public JmzAdminUserRoles findOneAdminUserRole(JmzAdminUserRoles jmzAdminUserRoles) throws DataAccessException{
        return adminUserRoleDao.findOne(jmzAdminUserRoles);
    }

    public Boolean deleteBatchAdminUserRole(JmzAdminUserRoles jmzAdminUserRoles) throws  DataAccessException{
        if(null == jmzAdminUserRoles.getAdminId()){//Prevent false deletion (防止错误批量删除)
            throw new BusinessException(ResponseCode.DB_LOSE_PARM.getCode(),
                    ResponseCode.DB_LOSE_PARM.getDesc());
        }
        adminUserRoleDao.delete(jmzAdminUserRoles);
        return true;
    }
}
